//
//  FundRecordController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/6/14.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import MJRefresh
import RxCocoa
import RxSwift

class FundRecordController: CBBaseViewController {
    
    var tbRecord:UITableView!
    var arrData:[FundRecordModel] = [FundRecordModel]()
    let CELL_ID = "FUND_RECORD_CELL_ID"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "资金记录"
        self.view.backgroundColor = UIColor(hex:0xf2f2f2)
        
        tbRecord = UITableView(frame: .zero, style: .plain)
        tbRecord.rowHeight = 95
        tbRecord.separatorStyle = .none
        tbRecord.dataSource = self
        tbRecord.tableFooterView = UIView()
        tbRecord.register(UINib(nibName: "FundRecordCell", bundle: nil), forCellReuseIdentifier: CELL_ID)
        tbRecord.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
            self?.curPage = 1
            self?.initData()
        })
        tbRecord.mj_footer = MJRefreshFooter(refreshingBlock: {  [unowned self] in
            self.curPage += 1
            self.initData()
        })
        tbRecord.mj_footer.isHidden = true
        self.view.addSubview(tbRecord)
        
        tbRecord.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        tbRecord.mj_header.beginRefreshing()
    }
    
    var curPage:Int = 1
    let pageSize:Int = 20
    func initData() {
        NetworkTool.requestData(url: ActionURL.GET_FUND_RECORD, param: [
            "uid":MainTools.shared.userModel?.uid ?? "",
            "page":curPage,
            "perPage":pageSize])
            .map { (json) -> [FundRecordModel]? in
                return FundRecordModel.mj_objectArray(withKeyValuesArray: json["rows"].dictionaryValue["list"]?.arrayObject) as? [FundRecordModel]
            }.catchErrorJustReturn(nil)
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self] tmpArr in
                self.tbRecord.mj_header.endRefreshing()
                self.tbRecord.mj_footer.endRefreshing()
                if let arrModel = tmpArr {
                    if self.curPage == 1 {
                        self.arrData = arrModel
                    } else {
                        self.arrData.append(contentsOf: arrModel)
                    }
                    self.tbRecord.mj_footer.isHidden = (arrModel.count < self.pageSize)
                    self.tbRecord.reloadData()
                }
            }).disposed(by: disposeBag)
    }
    
    
    

}

extension FundRecordController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:FundRecordCell = (tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as? FundRecordCell) ?? FundRecordCell()
        cell.model = arrData[indexPath.row]
        return cell
    }
    
}
